package com.pt.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.pt.common.enums.UserClass;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;

@Data
@TableName("user")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @TableId(type = IdType.AUTO)
    private Long id;
    
    private String username;
    
    @JsonIgnore  // 防止密码哈希值在JSON响应中暴露
    private String password;
    
    private String email;
    
    private Integer status;
    
    private Integer userClass;
    
    private Long uploaded;
    
    private Long downloaded;
    
    private Integer bonusPoints;
    
    private String invitationCode;
    
    private Long invitedBy;
    
    @JsonIgnore  // passkey也不应该在API响应中暴露
    private String passkey;
    
    private LocalDateTime createdTime;
    
    private LocalDateTime lastLoginTime;
    
    private String lastLoginIp;
    
    private String avatar;
    
    private String signature;
    
    private LocalDateTime assessmentDeadline;
    
    private Integer warningCount;
    
    @TableField(exist = false)
    private Integer dailyDownloadCount;  // 今日下载次数
    
    @TableField(exist = false)
    private Integer dailyDownloadLimit;  // 每日下载限制
    
    @TableField(exist = false)
    private String token;  // JWT token，不存储到数据库
    
    public Integer getDailyDownloadLimit() {
        UserClass userClass = UserClass.getByCode(this.userClass);
        if (userClass == null) {
            return DAILY_DOWNLOAD_LIMIT_NORMAL;
        }
        return userClass.getDailyDownloadLimit();
    }
    
    public static final int USER_CLASS_NORMAL = 1;    // 普通用户
    public static final int USER_CLASS_ADVANCED = 2;  // 高级用户
    public static final int USER_CLASS_VIP = 3;       // VIP用户
    public static final int USER_CLASS_ADMIN = 4;     // 管理员

    public static final int DAILY_DOWNLOAD_LIMIT_NORMAL = 5;    // 普通用户每日下载限制
    public static final int DAILY_DOWNLOAD_LIMIT_ADVANCED = 50; // 高级用户每日下载限制
    public static final int DAILY_DOWNLOAD_LIMIT_VIP = -1;      // VIP用户无限制
    public static final int DAILY_DOWNLOAD_LIMIT_ADMIN = -1;    // 管理员无限制

    public static final long UPLOAD_THRESHOLD_ADVANCED = 100L * 1024 * 1024 * 1024;  // 100GB
    public static final double RATIO_THRESHOLD_ADVANCED = 1.5;
    public static final int DAYS_THRESHOLD_ADVANCED = 30;

    public static final long UPLOAD_THRESHOLD_VIP = 500L * 1024 * 1024 * 1024;  // 500GB
    public static final double RATIO_THRESHOLD_VIP = 2.0;
    public static final int DAYS_THRESHOLD_VIP = 90;
    
    // 非数据库字段，用于显示
    public Double getShareRatio() {
        if (downloaded == null || downloaded == 0) {
            return uploaded != null && uploaded > 0 ? Double.MAX_VALUE : 0.0;
        }
        return uploaded != null ? uploaded * 1.0 / downloaded : 0.0;
    }
    
    /**
     * 判断是否为管理员
     */
    public boolean isAdmin() {
        return userClass != null && userClass == UserClass.ADMIN.getCode();
    }
} 